// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
      defaultImage: '/images/default-img.jpg',
      inTheaters:[],
      comingSoon:[],
      top250:[],
  },

  onLoad() {
    //构建影片信息请求
    var that = this;
    wx.request({
      url: app.globalData.gBaseUrl + 'in_theaters',
      data: {
          start: 0,
          count: 3
      },
      method: "GET",
      success:function(res) {
        // console.log(res);
        that.setData({
            inTheaters:res.data.subjects
        });
      }
    });
    wx.request({
        url: app.globalData.gBaseUrl + 'coming_soon',
        data: {
            start: 0,
            count: 3
        },
        method: "GET",
        success:function(res) {
        //   console.log(res);
          that.setData({
              comingSoon:res.data.subjects
          });
        }
      });
      wx.request({
        url: app.globalData.gBaseUrl + 'top250',
        data: {
            start: 0,
            count: 3
        },
        method: "GET",
        success:function(res) {
        //   console.log(res);
          that.setData({
              top250:res.data.subjects
          });
        }
      });
  },

  //跳转搜索页面
  searchInput:function() {
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },

  //海报获取失败时使用默认海报
  onError:function(e) {
    //   console.log(e)
      var index = e.currentTarget.dataset.index;
      var name = e.currentTarget.dataset.name;
      if (name == 'inTheaters') {
        var inTheaters = this.data.inTheaters;
        inTheaters[index].images.large = this.data.defaultImage;
        this.setData({inTheaters});
      };
      if (name == 'comingSoon') {
        var comingSoon = this.data.comingSoon;
        comingSoon[index].images.large = this.data.defaultImage;
        this.setData({comingSoon});
      };
      if (name == 'top250') {
        var top250 = this.data.top250;
        top250[index].images.large = this.data.defaultImage;
        this.setData({top250});
      };
  },

  //查看更多
  more:function(e) {
    console.log(e);
    var type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: '../more/more?type='+type,
    })
  },

})
